package com.example.administrator.waitan.hot.week;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.example.administrator.waitan.R;
import com.example.administrator.waitan.hot.today.todaycontent.TodayContentActivity;
import com.example.administrator.waitan.tools.GsonRequest;
import com.example.administrator.waitan.tools.VolleySingleton;
import com.google.gson.Gson;

import java.util.ArrayList;

/**
 * Created by Administrator on 2016/1/11.
 */
public class WeekFragment extends Fragment {
    private RequestQueue queue;
    private ArrayList<WeekInfo> infos;
    private Context context;
    private ListView mListView;
    private WeekBaseAdapter mAdapter;
    private StringRequest request;
    private String url = "http://dailyapi.ibaozou.com/api/v1/section/58";

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_week, null);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mListView = (ListView) getActivity().findViewById(R.id.lv_week);
        GsonRequest<WeekBeans> weekBeansGsonRequest = new GsonRequest<WeekBeans>(url, WeekBeans.class, new Response.Listener<WeekBeans>() {
            @Override
            public void onResponse(final WeekBeans response) {
                mAdapter = new WeekBaseAdapter(response.getArticles(),getActivity());
                mListView.setAdapter(mAdapter);
                mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                        Intent intent = new Intent(getActivity(), TodayContentActivity.class);
                        intent.putExtra("ID",response.getArticles().get(i).getShare_url());
                        startActivityForResult(intent,200);
                    }
                });

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        VolleySingleton.getInstance()._addRequest(weekBeansGsonRequest);
    }
}
